<?php

namespace app\admin\model\tongcheng;

use addons\tongcheng\enum\Gender;
use addons\tongcheng\enum\RiderEnterStatus;
use addons\tongcheng\enum\RiderWorkStatus;
use addons\tongcheng\enum\UserRole;
use addons\tongcheng\model\BaseModel;
use addons\tongcheng\model\UserModel;
use app\common\library\Auth;
use fast\Random;
use traits\model\SoftDelete;

class DriveDocModel extends BaseModel
{

    use SoftDelete;


    // 表名
    protected $name = 'tongcheng_rider_enter';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text'
    ];

    public function getGenderList()
    {
        return ['1' => __('Gender 1'), '0' => __('Gender 0')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list  = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function zone()
    {
        return $this->belongsTo('Zone', 'zone_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    /**
     * 创建骑手信息
     * @param $rider_enter
     * @return RiderModel
     */
    public static function createRider($rider_enter)
    {
        if ($rider_enter['status'] != RiderEnterStatus::PASSED) {
            return null;
        }

        $rider = RiderModel::get([
            // 'zone_id' => $rider_enter['zone_id'],
            'user_id' => $rider_enter['user_id'],
        ]);
        // 创建骑手信息（不同区域同一骑手信息）
        if (!$rider) {
            $rider = RiderModel::create([
                'id'                     => $rider_enter['user_id'],// 骑手id与用户id一致
                'zone_id'                => $rider_enter['zone_id'],
                'user_id'                => $rider_enter['user_id'],
                'real_name'              => $rider_enter['real_name'],
                'mobile'                 => $rider_enter['mobile'],
                'gender'                 => $rider_enter['gender'] ?? Gender::MALE,
                'id_card_no'             => '', //$rider_enter['id_card_no'],
                'work_status'            => RiderWorkStatus::WORKING,
                'status'                 => 1,
                'day_order_limit'        => 0,
                'taking_order_limit'     => 0,
                'today_order_count'      => 0,
                'taking_order_count'     => 0,
                'zone_delivery_fee_rate' => ZoneConfigModel::getZoneDeliveryFeeRate($rider_enter['zone_id']),// 设置区域配送费抽佣比例
            ]);
        }

        // 更新用户角色
        \addons\tongcheng\model\UserInfoModel::where('user_id', $rider_enter['user_id'])->update([
            'role' => UserRole::RIDER,
        ]);

        // 设置骑手默认账号、密码
        $user = UserModel::get($rider_enter['user_id']);
        if ($user) {
            $user->username = $rider_enter['mobile'];       // 手机号作为用户名
            $user->salt     = $rider_enter['salt'] ?? Random::alnum();
            $user->password = $rider_enter['password'] ?? Auth::instance()->getEncryptPassword($rider_enter['mobile'], $user->salt);// 密码（手机号作为密码）
            $user->mobile   = $rider_enter['mobile'];
            $user->gender   = $rider_enter['gender'];
            $user->save();
        }

        return $rider;
    }
}
